﻿using CloudChartLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPFCustomControls.views
{
    /// <summary>
    /// CloudChart.xaml 的交互逻辑
    /// </summary>
    public partial class CloudChartPage : Page
    {
        public CloudChartPage()
        {
            InitializeComponent();



            cloudChartData = new double[row, col];
            Random random = new Random();
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                {
                    cloudChartData[i, j] = random.NextDouble()*100;
                }
            }


            this.cloudChartCanvas.SizeChanged += new SizeChangedEventHandler(CloudChartCanvas_SizeChange);
            this.cloudChartCanvas2.SizeChanged += new SizeChangedEventHandler(CloudChartCanvas_SizeChange2);
        }




        #region 云图

        private void CloudChartCanvas_SizeChange(object sender, SizeChangedEventArgs e)
        {
            CloudChartResize();
            DisplayCloudChart();
        }


        int row = 6;
        int col = 6;
        private double[,] cloudChartData;
        ColorExpand colorExpand = new ColorExpand();
        CloudChart cloudChart;
        WriteableBitmap graphBitmap;
        byte[] buffer;
        int imageWidth;
        int imageHeight;
        private void DisplayCloudChart()
        {
            
            if (IsZeroArray(cloudChartData)) return;
            cloudChart.CreateChart(cloudChartData, InterpolateMethod.BiCubic, (row, col, c) =>
            {
                Array.Copy(new byte[] { c.B, c.G, c.R }, 0, buffer, row * graphBitmap.BackBufferStride + col * 3, 3);
            });
          

            graphBitmap.WritePixels(new Int32Rect(0, 0, imageWidth, imageHeight), buffer, graphBitmap.BackBufferStride, 0);
            cloudChartImage.Source = graphBitmap;
        }

        public bool IsZeroArray(double[,] array)
        {
            for (int i = 0; i < array.GetLength(0); i++)
            {
                for (int j = 0; j < array.GetLength(1); j++)
                {
                    if (array[i, j] != 0)
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        private void CloudChartResize()
        {
            imageWidth = (int)cloudChartCanvas.ActualWidth;
            imageHeight = (int)cloudChartCanvas.ActualHeight;
            cloudChartImage.Width = imageWidth;
            cloudChartImage.Height = imageHeight;
            cloudChart = new CloudChart(imageWidth, imageHeight, colorExpand.Colors);

            graphBitmap = new WriteableBitmap(imageWidth, imageHeight, 36, 36, PixelFormats.Bgr24, null);

            buffer = new byte[graphBitmap.BackBufferStride * graphBitmap.PixelHeight];
        }
        #endregion



        #region 云图2

        private void CloudChartCanvas_SizeChange2(object sender, SizeChangedEventArgs e)
        {
            CloudChartResize2();
            DisplayCloudChart2();
        }

        CloudChart cloudChart2;
        WriteableBitmap graphBitmap2;
        byte[] buffer2;
        int imageWidth2;
        int imageHeight2;
        private void DisplayCloudChart2()
        {
            if (IsZeroArray(cloudChartData)) return;
            cloudChart2.CreateChart(cloudChartData,0,100, InterpolateMethod.BiCubic, (row, col, c) =>
            {
                Array.Copy(new byte[] { c.B, c.G, c.R }, 0, buffer2, row * graphBitmap2.BackBufferStride + col * 3, 3);
            });


            graphBitmap2.WritePixels(new Int32Rect(0, 0, imageWidth2, imageHeight2), buffer2, graphBitmap2.BackBufferStride, 0);
            cloudChartImage2.Source = graphBitmap2;
        }

        private void CloudChartResize2()
        {
            imageWidth2 = (int)cloudChartCanvas2.ActualWidth;
            imageHeight2 = (int)cloudChartCanvas2.ActualHeight;
            cloudChartImage2.Width = imageWidth2;
            cloudChartImage2.Height = imageHeight2;
            cloudChart2 = new CloudChart(imageWidth2, imageHeight2, colorExpand.Colors);

            graphBitmap2 = new WriteableBitmap(imageWidth2, imageHeight2, 36, 36, PixelFormats.Bgr24, null);

            buffer2 = new byte[graphBitmap2.BackBufferStride * graphBitmap2.PixelHeight];
        }
        #endregion
    }
}
